package com.sunyard.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ：kaik.lou
 * @date ： 2020/7/16 16:27
 * @description：java8版本的时间计算
 */
public class DateUtilPro {

    /**
     * @FIELD: TODO
     */
    public final static String DD = "dd";
    /**
     * @FIELD: TODO
     */
    public final static String MM = "MM";
    /**
     * @FIELD: TODO
     */
    public final static String HHMMSS = "HHmmss";
    /**
     * @FIELD: TODO
     */
    public final static String YYYY = "yyyy";
    /**
     * @FIELD: TODO
     */
    public final static String YYYYMM = "yyyyMM";
    public final static String YYYY_MM = "yyyy-MM";
    /**
     * @FIELD: TODO
     */
    public final static String YYYYMMDD = "yyyyMMdd";
    /**
     * @FIELD: TODO
     */
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    /**
     * @FIELD: TODO
     */
    public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public final static String YYYYMMDDHH24MMSS = "yyyyMMdd" + " HHmmss";
    /**
     * @FIELD: TODO
     */
    public final static String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    /**
     * @FIELD: TODO
     */
    public final static String HH_MM_SS_SSS = "HH:mm:ss.SSS";
    /**
     * @FIELD: TODO
     */
    public final static String HH_MM_SS = "HH:mm:ss";
    /**
     * @FIELD: TODO
     */
    public final static String YYYYMMDD_HHMMSSSSS = "yyyy-MM-dd-HH.mm.ss.SSSSSS";
    /**
     * @FIELD: TODO
     */
    public final static String HHMMSSSSS = "HHmmssSSS";

    /**
     * 将时间格式化成字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String getDateStr(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 将字符串格式化成时间格式
     *
     * @param format
     * @param str
     * @return LocalDate
     */
    public static LocalDate getLocalDate(String format, String str) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        LocalDate parse = LocalDate.parse(str, dtf);
        return parse;
    }

    /**
     * 将字符串格式化成时间格式
     *
     * @param format
     * @param str
     * @return LocalDate
     */
    public static LocalDateTime getLocalDateTime(String format, String str) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        LocalDateTime parse = LocalDateTime.parse(str, dtf);
        return parse;
    }

    /**
     * 将字符串格式化成时间格式
     *
     * @param format
     * @param ldt
     * @return
     */
    public static String getStringByLocalDate(String format, LocalDate ldt) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        String dataTime_ = ldt.format(dtf);
        return dataTime_;
    }

    /**
     * 将字符串解析成Date
     *
     * @param format
     * @param
     * @return
     * @throws ParseException
     */
    public static Date getDate(String format, String str) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 将字符串格式化成时间格式
     *
     * @param format
     * @param ldt
     * @return
     */
    public static String getStringByLocalDateTime(String format, LocalDateTime ldt) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        String dataTime_ = ldt.format(dtf);
        return dataTime_;
    }

    /**
     * @param @param  smdate
     * @param @param  bdate
     * @param @return
     * @param @throws ParseException
     * @return int
     * @throws
     * @Title: monthsBetween
     * @Description: 两个月份相差年
     */
    public static long dyearsBetween(Date smdate, Date bdate) throws ParseException {

        String startTime = getDateStr(DateUtilPro.YYYY_MM_DD, smdate);
        String endTime = getDateStr(DateUtilPro.YYYY_MM_DD, bdate);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        long year = ChronoUnit.YEARS.between(startDate, endDate);

        return year;
    }

    /**
     * @param @param  smdate
     * @param @param  bdate
     * @param @return
     * @param @throws ParseException
     * @return int
     * @throws
     * @Title: monthsBetween
     * @Description: 两个月份相差月数
     */
    public static long monthsBetween(Date smdate, Date bdate) throws ParseException {

        String startTime = getDateStr(DateUtilPro.YYYY_MM_DD, smdate);
        String endTime = getDateStr(DateUtilPro.YYYY_MM_DD, bdate);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        // 日期区间
//        long days = ChronoUnit.DAYS.between(startDate, endDate);
        //月
        long month = ChronoUnit.MONTHS.between(startDate, endDate);
//        //年
//        long year = ChronoUnit.YEARS.between(startDate, endDate);
        return month;
    }

    public static long monthsBetweenPro(LocalDate startDate, LocalDate endDate) throws ParseException {
        // 日期区间
//        long days = ChronoUnit.DAYS.between(startDate, endDate);
        //月
        long month = ChronoUnit.MONTHS.between(startDate, endDate);
//        //年
//        long year = ChronoUnit.YEARS.between(startDate, endDate);
        return month;
    }

    /**
     * @param @param  smdate
     * @param @param  bdate
     * @param @return
     * @param @throws ParseException
     * @return int
     * @throws
     * @Title: monthsBetween
     * @Description: 两个月份相差天数
     */
    public static long daysBetween(Date smdate, Date bdate) throws ParseException {

        String startTime = getDateStr(DateUtilPro.YYYY_MM_DD, smdate);
        String endTime = getDateStr(DateUtilPro.YYYY_MM_DD, bdate);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        // 日期区间
        long days = ChronoUnit.DAYS.between(startDate, endDate);

//        //年
//        long year = ChronoUnit.YEARS.between(startDate, endDate);

        return days;
    }


    /**
     * 获取某年某月有多少天
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDayOfMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);

    }
}
